<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2019/3/15
 * Time: 16:43
 */

namespace app\api\service;
use app\api\model\Category;
use app\api\model\Coupon as CouponModel;
use app\api\model\CouponCode;
use app\api\model\CouponQrcode;
use app\api\model\Product;
use app\lib\exception\CouponException;
use app\lib\exception\MiniSuiteException;
use think\Cache;
use think\Config;
use think\Db;
use think\Exception;
use think\model\Collection;

class Coupon
{
    protected  $cahe='';
    function __construct()
    {
        //初始化redis链接
        $this->cache = Cache::connect(Config::get('cache.redis'));
        $this->cache->handler()->select(10);
    }

    /**
     * 获取优惠卷列表
     * @param $t_id
     * @param $wu_id
     * @param $uid
     * @return array|mixed
     * @throws CouponException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($t_id,$wu_id,$uid){
        $array = [];
        //获取所有准备的优惠卷
        $couponList = [];
        $list=[];
        $this->cache->handler()->setOption( $this->cache->handler()::OPT_SCAN, $this->cache->handler()::SCAN_RETRY);
        $iterator = null;
        while ($keys = $this->cache->handler()->scan($iterator, "coupon:detail:t_id:".$t_id.":c_id:*")) {
            foreach ($keys as $item){
                array_push($list, $item);
            }
        }
        //查询用户已有列表
        $where['is_use'] = 0;
        $userCode = CouponCode::getCodeListByUserId($uid,$where);
        if (!$userCode->isEmpty()){
            $array = $userCode->toArray();
            $array = array_column($array,'c_id');
        }
        foreach ($list as $key=>$value){
            $coupon =  $this->cache->handler()->hgetall($value);
            if ($coupon['valid_type'] == 2){
                if(!empty($coupon['valid_end_time']) && strtotime($coupon['valid_end_time'])<time() ){//判断优惠卷是否过期
                    $this->batchUpdateCoupon($t_id,$coupon['id'],'down');//过期下架
                }
            }
            if (isset($coupon['own_user']) && $coupon['own_user']!= $wu_id && $coupon['own_user'] !=0){
                continue;//跳出本次循环
            }
            $couponList[$key] = $coupon;
            $couponList[$key]['own'] = 0;
            if (in_array($coupon['id'],$array)){
                $couponList[$key]['own'] = 1;
            }
        }
        if (!empty($couponList)){
            $couponList = $this->getSort($couponList);//数组初始化
            $couponList =  $this->getCouponSort($couponList);//数组排序
        }
        return $couponList;
    }

    /**
     * 获取所有驾校优惠卷
     * @param $t_id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponListByTid($t_id){
        //获取所有准备的优惠卷
        $couponList = [];
        $where = 'putaway = 2 and own_user = 0';//获取已上架的优惠卷
        $coupon = new CouponModel();
        $couponList = $coupon->getCouponList($t_id,$where);
        foreach ($couponList as $item){
            $item->hidden(['style','putaway','update_user','delete_time']);
        }
        return $couponList;
    }

    /**
     * 创建优惠卷二维码
     * @param $wu_id
     * @param $c_id
     * @param $page
     * @return CouponQrcode
     * @throws \Exception
     */
    public function getCouponQRCode($wu_id,$t_id,$c_id,$page){
        $qrcode = CouponQrcode::getUrl($wu_id,$c_id);
        if (!empty($qrcode)){
            $status['id'] = $qrcode->id;
            $status['key'] = config('setting.remote_img_url').$qrcode->url;
            return  $status;
        }
        $poster = new Poster();
        $scene = 'sowcode_'.$wu_id.'&'.'c_'.$c_id;
        list($data, $path, $name, $result) =  $poster->getQRCodeImg($t_id,$wu_id,$scene,$page);
        if ($data != null || !$result){
            throw new MiniSuiteException([
                'code'=>'201',
                'msg'=>'获取二维码'.$data->errmsg,
                'error_code'=>$data->errcode
            ]);
        }
        $info['wu_id'] = $wu_id;
        $info['c_id'] = $c_id;
        $info['url'] = $result['key'];
        $qrcode = CouponQrcode::createQrcode($info);
        $status['id'] = $qrcode->id;
        $status['key'] = config('setting.remote_img_url').$qrcode->url;
      return $status;
    }

    /**
     * 获客详情
     * @param $c_id
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getQRcodeUser($c_id,$wu_id){
        $info = [];
        $result = CouponCode::getUsers($c_id,$wu_id);
        $result->toArray();
        foreach ($result as $key=> $item){
            if (!empty($item['user']) ){
                $info[$key] = $item['user'];
            }
        }
        return $info;
    }

    /**
     * 获取用户优惠卷
     * @param $t_id
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserCoupon($t_id,$wu_id){
        $where = '1 = 1';
        $coupon = CouponModel::getUserCoupon($t_id,$wu_id,$where);
        return $coupon;
    }

    /**
     * 查询商品已有优惠卷
     * @param $uid
     * @param $t_id
     * @param $product_id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListByProduct($uid,$wu_id,$t_id,$product_id){
        $array=[];
        $couponList='';
        $this->cache->handler()->setOption( $this->cache->handler()::OPT_SCAN, $this->cache->handler()::SCAN_RETRY);
        $iterator = null;
        $list = [];
        while ($keys = $this->cache->handler()->scan($iterator, "coupon:detail:t_id:".$t_id.":c_id:*")) {
            foreach ($keys as $item){
                array_push($list, $item);
            }
        }
        //查询用户已有列表
        $where['is_use'] = 0;
        $userCode = CouponCode::getCodeListByUserId($uid,$where);
        if (!$userCode->isEmpty()){
            $array = $userCode->toArray();
            $array = array_column($array,'c_id');
        }
        $product = Product::getProduct($product_id);
        foreach ($list as $key=>$value){
            $coupon =  $this->cache->handler()->hgetall($value);
            if ($coupon['used'] == 1){
                if (isset($coupon['own_user']) && $coupon['own_user']!= $wu_id && $coupon['own_user'] !=0){
                    continue;//跳出本次循环
                }
                $couponList[$key] = $coupon;
                $couponList[$key]['own'] = 0;
                if (in_array($coupon['id'],$array)){
                    $couponList[$key]['own'] = 1;
                }
            }
            if ($coupon['used'] == 2){
                if ($product->category_id == $coupon['with_sn']){
                    $couponList[$key] = $coupon;
                    $couponList[$key]['own'] = 0;
                    if (in_array($coupon['id'],$array)){
                        $couponList[$key]['own'] = 1;
                    }
                }
            }
            if ($coupon['used'] == 3){
                $goods = explode(',',$coupon['with_sn']);
                if (in_array($product->id,$goods)){
                    $couponList[$key] = $coupon;
                    $couponList[$key]['own'] = 0;
                    if (in_array($coupon['id'],$array)){
                        $couponList[$key]['own'] = 1;
                    }
                }
            }
        }
        if (!empty($couponList)) {
            $couponList = $this->getSort($couponList);//数组初始化
            $couponList = $this->getCouponSort($couponList);//数组排序
        }
        return $couponList;
    }

    /**
     * 获取优惠卷详情
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponDetail($id){
        $coupon = new CouponModel();
        $result = $coupon->getCouponById($id);
        if (!empty($result)){
            $result = $result->toArray();
            if ($result['valid_type'] == 2 || $result['putaway'] == 1){
                if (strtotime($result['valid_end_time'])<= time() || $result['putaway'] == 1){
                    $result['expire'] = 'YES';
                }else{
                    $result['expire'] = 'NO';
                }
            }else{
                //领卷后过期
                $result['expire'] = 'NO';
            }
        }
        return $result;
    }
    /**
     * 领取购物券
     * @param $uid
     * @param $w_id
     * @param $t_id
     * @param $c_id
     * @return $this|bool
     * @throws CouponException
     */
    public function getCoupon($uid,$w_id,$t_id,$c_id){
        $num = $this->cache->handler()->hget('user_coupon:t_id:'.$t_id,$uid."_".$c_id)?1:0;
        if ($num == 1){
            throw new CouponException([
                'code'=>201,
                'msg'=>'你已经拥有此优惠卷了',
                'error_code'=>90030
            ]);
        }
        $codelenght =  $this->cache->handler()->llen("coupon:code:t_id:".$t_id.":c_id:".$c_id);
        if ($codelenght == 0){
            $this->unGroundingCoupon($c_id,$t_id);//删除已领完的优惠券
            $this->cache->handler()->del('user_coupon:t_id:'.$t_id);//删除领取队列释放资源
            throw new CouponException([
               'code'=>201,
                'msg'=>'该优惠券已被抢光了',
                'error_code'=>90020
            ]);
        }
        $code =  $this->cache->handler()->lpop("coupon:code:t_id:".$t_id.":c_id:".$c_id);
        $result =  $this->updateCode($c_id,$uid,$code,$w_id);
        if (!$result){//失败则重新入库
            $this->cache->handler()->rpush("coupon:code:t_id:".$t_id.":c_id:".$c_id,$code);
            return $result;
        }
        $this->cache->handler()->hset('user_coupon:t_id:'.$t_id,$uid."_".$c_id,1);
        return $result;
    }

    /**
     * 获取用户优惠卷
     * @param $uid
     * @param $type
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponByUser($uid,$type){
        $result = CouponCode::getCouponByUserId($uid);
        if ($type =='used'){//已使用
           $result =  $this->getUsedCoupon( $result);
        }elseif ($type =='notUsed'){//未使用的
            $result =  $this->getUnUseCoupon($result);
        }elseif ($type == 'expired'){//过期的
            $result =  $this->getExpiredCoupon($result);
        }
        if (!empty($result)){
        $result = $this->getSort($result);//数组初始化
        }
        return $result;
    }

    /**
     * 获取已使用优惠券
     * @param $result
     * @param $type
     * @return string
     */
    public function getUsedCoupon(Collection $result){
        $array ='';
        foreach ($result as $key=>$value){
            if ($value->is_use == 1){
                $array[$key] = $value;
            }
        }
        return $array;
    }

    /**
     * 获取未使用的购物券
     * @param CouponCode $result
     * @return string
     */
    public function getUnUseCoupon(Collection $result){
        $array = '';
        foreach ($result as $key=>$value){
            if ($value->coupon->valid_type == 1){
                $end_time = (strtotime($value->update_time) + $value->coupon->valid_days*24*3600);
                if (time()<$end_time){
                    $array[$key] = $value;
                    $array[$key]['end_time'] = date("Y-m-d H:i:s",$end_time);
                }
            }
            if ($value->coupon->valid_type == 2){
                if (time()< strtotime($value->coupon->valid_end_time)){
                    $array[$key] = $value;
                }
            }
        }
        return $array;
    }

    /**
     * 获取未使用且过期
     * @param CouponCode $result
     * @return string
     */
    public function getExpiredCoupon(Collection $result){
        $array = '';
        foreach ($result as $key=>$value){
            if ($value->coupon->valid_type == 1){
                if (time()>((strtotime($value->update_time) + $value->coupon->valid_days*24*3600))){
                    $array[$key] = $value;
                }
            }
            if ($value->coupon->valid_type == 2){
                if (time()> strtotime($value->coupon->valid_end_time)){
                    $array[$key] = $value;
                }
            }
        }
        return $array;
    }
    /**
     * 更新优惠券code表
     * @param $uid
     * @param $code
     * @param $w_id
     * @return $this|bool
     */
    public function updateCode($c_id,$uid,$code,$w_id){
        Db::startTrans();
        try{
            $data['user_id'] = $uid;
            $data['source'] = $w_id;
            $data['update_time'] = time();
            $result = CouponCode::updateCode($code,$data);
            $result = CouponModel::incNum($c_id);
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            $result = false;
        }
        return $result;
    }

    /**
     * 添加优惠卷
     * @param $data
     * @param $t_id
     * @return int|string
     * @throws CouponException
     */
    public function addCoupon($data,$t_id,$wu_id = 0){
        $info['name'] = $data['name'];//优惠券名称
        $info['quota'] = $data['quota'];//优惠券张数
        $info['line'] = $data['line'];//优惠券张数
        $info['putaway'] = $data['putaway'];//是否上架
        $info['used'] = $data['used'];//优惠券用途
        $info = $this->getData($data, $t_id, $info);
        $info['create_user'] = $t_id;
        $info['own_user'] = $wu_id;
        $info['create_time'] = time();
        Db::startTrans();
        try{
            $coupon = new CouponModel();
            $coupon_id = $coupon->addCoupon($info);
            //激活码处理
            $result =  $this->makeCoupon($coupon_id,$data['quota']);
            if ($info['putaway'] == 2){
                $this->groundingCoupon($coupon_id,$t_id);
            }
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            throw new CouponException([
                'code'=>201,
                'msg'=>'优惠券添加失败',
                'error_code'=>90006
            ]);
        }
        return $result;
    }

    /**
     * 更新优惠卷
     * @param $data
     * @param $t_id
     * @return CouponModel
     * @throws CouponException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateCoupon($data,$t_id){
        $coupon = new CouponModel();
        $old = $coupon->getCouponById($data['id']);
        $old['putaway'] = $old->putaway;
        $info['id'] = $data['id'];
        $info['name'] = $data['name'];//优惠券名称
        $info['line'] = $data['line'];//优惠券张数
        $info['quota'] = $data['quota'];//优惠券张数
        $info['putaway'] = $data['putaway'];//是否上架
        if ($info['putaway'] == 1){
            $this->unGroundingCoupon($data['id'],$t_id);
        }
        if ($old['putaway'] == 1 && $data['putaway'] == 2){
            $info['grounding_time'] =time();
        }
        $info['used'] = $data['used'];//优惠券用途
        $info = $this->getData($data, $t_id, $info);
        $info['create_user'] = $t_id;
        $info['create_time'] = time();
        $result = $coupon->updateCoupon($info);
        return $result;
    }

    /**
     * 优惠卷上下架
     * @param $t_id
     * @param $ids
     * @param $type
     * @return CouponModel
     * @throws CouponException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function batchUpdateCoupon($t_id,$ids,$type){
        $coupon = new CouponModel();
        $couponInfo = $coupon->getCouponById($ids);
        if ($type =='up'){
            $num = $couponInfo->quota - $couponInfo->take_count;
            if ($num == 0){
                throw new CouponException([
                    'code'=>201,
                    'msg'=>'优惠券已领完，不能上架',
                    'error_code'=>90010
                ]);
            }
            $this->groundingCoupon($ids,$t_id);
            $data['putaway'] = 2;
            $data['grounding_time'] = time();
        }elseif ($type =='down'){
            $this->unGroundingCoupon($ids,$t_id);//下架
            $data['putaway'] = 1;
        }
        $result = $coupon->batchUpdateCoupon($ids,$data);
        return $result;
    }

    /**
     * 删除优惠卷
     * @param $id
     * @return int
     * @throws CouponException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function deleteCoupon($id){
        $coupon = new CouponModel();
        $data = $coupon->getCouponById($id);
        if ($data->putaway == 2){
            throw new CouponException([
               'code'=>201,
                'msg'=>'优惠券未下架，不能删除',
                'error_code'=>90009
            ]);
        }
        $result = $coupon->deleteCoupon($id);
        return $result;
    }

    /**
     * 获取名片列表
     * @param $t_id
     * @param $type
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponList($t_id,$type){
        $where = '';
        if ($type == 'all'){
            $where = '1 = 1';
        }elseif ($type == 'grounding'){
            $where = 'putaway = 1';
        }elseif ($type =='shelves'){
            $where = 'putaway = 2';
        }elseif ($type =='sold_out'){
            $where = 'quota = take_count';
        }
        $coupon = new CouponModel();
        $result = $coupon->getCouponList($t_id,$where);
        return $result;
    }

    /**
     * 获取优惠卷详情
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCouponById($id){
        $coupon = new CouponModel();
        $result = $coupon->getCouponById($id);
        $result = $result->toArray();
        if ($result['used'] == 2 ){
            $categody = Category::getCategory($result['with_sn']);
            $result['with_sn'] = [$categody->id => $categody->name];
        }elseif ($result['used'] == 3){
            $array = explode(',',$result['with_sn']);
            $product = Product::where('id','in',$array)->select();
            foreach ($product as $value){
                $info[$value->id] =$value->name;
            }
            $result['with_sn']  = $info;
        }
        return $result;
    }

    /**
     * 优惠卷初始化
     * @param $c_id
     * @param $t_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function groundingCoupon($c_id,$t_id){
        $codeList = CouponCode::getCodeList($c_id);
        $coupon = (new CouponModel())->getCouponById($c_id);
        //事务
        $this->cache->handler()->multi();
        $name = "coupon:code:t_id:".$t_id.":c_id:".$c_id;
        $coupon_name = "coupon:detail:t_id:".$t_id.":c_id:".$c_id;
        foreach ($codeList as $value){
            $this->cache->handler()->lpush($name,$value->code);
        }
        //redis描述coupon
        $this->cache->handler()->hset($coupon_name,'id',$coupon->id);
        $this->cache->handler()->hset($coupon_name,'name',$coupon->name);
        $this->cache->handler()->hset($coupon_name,'line',$coupon->line);
        $this->cache->handler()->hset($coupon_name,'used',$coupon->used);
        $this->cache->handler()->hset($coupon_name,'with_sn',$coupon->with_sn);
        $this->cache->handler()->hset($coupon_name,'type',$coupon->type);
        $this->cache->handler()->hset($coupon_name,'with_amount',$coupon->with_amount);
        $this->cache->handler()->hset($coupon_name,'used_amount',$coupon->used_amount);
        $this->cache->handler()->hset($coupon_name,'describe',$coupon->describe);
        $this->cache->handler()->hset($coupon_name,'valid_type',$coupon->valid_type);
        $this->cache->handler()->hset($coupon_name,'valid_start_time',$coupon->valid_start_time);
        $this->cache->handler()->hset($coupon_name,'valid_end_time',$coupon->valid_end_time);
        $this->cache->handler()->hset($coupon_name,'valid_days',$coupon->valid_days);
        $this->cache->handler()->hset($coupon_name,'own_user',$coupon->own_user);
        $result =  $this->cache->handler()->exec();
        return $result;
    }
    /**
     * 优惠券下架
     * @param $t_id
     * @param $type
     * @return bool
     */
    public function unGroundingCoupon($c_id,$t_id){
        $name = "coupon:code:t_id:".$t_id.":c_id:".$c_id;
        $coupon_name = "coupon:detail:t_id:".$t_id.":c_id:".$c_id;
        $result =  $this->cache->handler()->del($name);
        $result = $this->cache->handler()->del($coupon_name);
        return $result;
    }

    /**
     * 数据
     * @param $data
     * @param $t_id
     * @param $info
     * @return mixed
     * @throws CouponException
     */
    private function getData($data, $t_id, $info)
    {
        if ($data['used'] == 1) {
            $info['with_sn'] = $t_id;
        } elseif ($data['used'] == 2) {
            $info['with_sn'] = $data['shop_arr'][0];
        } elseif ($data['used'] == 3) {
            $info['with_sn'] = implode(',', $data['shop_arr']);
        }
        //优惠券类容
        if ($data['shape']['type'] == 1) {
            $info['type'] = $data['shape']['type'];
            $info['with_amount'] = $data['shape']['fill'];
            $info['used_amount'] = $data['shape']['subtract'];
        } elseif ($data['shape']['type'] == 2) {
            $info['type'] = $data['shape']['type'];
            $info['used_amount'] = $data['shape']['money'];
        } elseif ($data['shape']['type'] == 3 || $data['shape']['type'] == 4) {
            $info['type'] = $data['shape']['type'];
            $info['describe'] = $data['shape']['describe'];
        }
        //时间
        if ($data['time']['type'] == 1) {
            $info['valid_type'] = $data['time']['type'];
            $info['valid_days'] = $data['time']['day'];
        } elseif ($data['time']['type'] == 2) {
            $info['valid_type'] = $data['time']['type'];
            $info['valid_start_time'] = $data['time']['start'];
            $info['valid_end_time'] = $data['time']['end'];
            if (strtotime( $info['valid_start_time'] ) >= strtotime( $info['valid_end_time'])){
                throw new CouponException([
                    'code'=>201,
                    'msg'=>'时间填写错误，开始时间必须小于结束时间',
                    'error_code'=>90006
                ]);
            }
        }
        return $info;
    }

    /**
     * 创建优惠卷
     * @param $c_id
     * @param $num
     * @return array|false
     * @throws \Exception
     */
    private function makeCoupon($c_id,$num){
        $info = '';
        $array =  $this->makeCode($num,'',8,'');
        foreach ($array as $key=>$value){
            $info[$key]['c_id'] = $c_id;
            $info[$key]['code'] = $value;
        }
        $result =  CouponCode::addCode($info);
        return $result;
    }
    /**
     * 优惠券激活码生成函数
     * @param $nums
     * @param string $exist_array
     * @param int $code_length
     * @param string $prefix
     * @return array
     */
    public function makeCode($nums,$exist_array='',$code_length = 6,$prefix = ''){
            $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz";
            $promotion_codes = array();//这个数组用来接收生成的优惠码
            for($j = 0 ; $j < $nums; $j++) {
                $code = '';
                for ($i = 0; $i < $code_length; $i++) {
                    $code .= $characters[mt_rand(0, strlen($characters)-1)];
                }
                //如果生成的4位随机数不再我们定义的$promotion_codes数组里面
                if( !in_array($code,$promotion_codes) ) {
                    if( is_array($exist_array) ) {
                        if( !in_array($code,$exist_array) ) {//排除已经使用的优惠码
                            $promotion_codes[$j] = $prefix.$code; //将生成的新优惠码赋值给promotion_codes数组
                        } else {
                            $j--;
                        }
                    } else {
                        $promotion_codes[$j] = $prefix.$code;//将优惠码赋值给数组
                    }
                } else {
                    $j--;
                }
            }
            return $promotion_codes;
    }

    /**
     * 数组重新排序
     * @param $array
     * @return array
     */
        public function getSort($array){
            $list = [];
            foreach ($array as $value){
                $list[] = $value;
            }
            return $list;
        }

    /**
     * 数组排序
     * @param $couponList
     * @return mixed
     */
    private function getCouponSort($couponList)
    {
        $own = array_column($couponList, 'own');
        array_multisort($own, SORT_ASC, $couponList);
        return $couponList;
    }

}